package com.tdam2011.grupo06.gui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tdam2011.grupo06.R;
import com.tdam2011.grupo06.control.GestorHistorialContacto;
import com.tdam2011.grupo06.model.Contacto;
import com.tdam2011.grupo06.model.ItemEmail;
import com.tdam2011.grupo06.model.ItemHistorial;
import com.tdam2011.grupo06.model.Llamada;
import com.tdam2011.grupo06.model.Mensaje;
import com.tdam2011.grupo06.model.MensajeBob;

public class HistorialContacto extends ListActivity
{
	private LinearLayout linearLay;
	private ItemHistorialAdapter adapter;
	private int contactoID;
	private CargarHistorialContactoTask cargarHistorialContactoTask;
	private GestorHistorialContacto gestorHistorialContacto;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contacto_historial);
		adapter = new ItemHistorialAdapter();
		linearLay = (LinearLayout) findViewById(R.id.linearLayoutHistorialContacto);
		
		setListAdapter(adapter);
		contactoID = getIntent().getExtras().getInt("idContacto");
		gestorHistorialContacto = new GestorHistorialContacto(adapter, HistorialContacto.this);
		cargarHistorialContactoTask = new CargarHistorialContactoTask();
		cargarHistorialContactoTask.execute("");
	}
	
	public class ItemHistorialAdapter extends BaseAdapter 
	{
		private ArrayList<ItemHistorial> itemsHistorial;
		private LayoutInflater inflater;

		public ItemHistorialAdapter() 
		{
			itemsHistorial = new ArrayList<ItemHistorial>();
			inflater = LayoutInflater.from(HistorialContacto.this);
		}

		public void addItemInfo(ItemHistorial info) {
			if (info != null) {
				itemsHistorial.add(info);
			}
		}

		public int getCount() {
			return itemsHistorial.size();
		}

		public Object getItem(int position) {
			return itemsHistorial.get(position);
		}

		public long getItemId(int position) {
			return position;
		}
		
		public void ordenarHistorial(Comparator<ItemHistorial> c)
		{
			Collections.sort(itemsHistorial, c);
		}
		
		class Holder 
		{
			private TextView txtTel;
			private ImageView imgTipo;
			private TextView txtFecha;
			private ImageView imgSubTipo;
		}

		public View getView(int position, View convertView, ViewGroup arg2) 
		{
			Holder holder;
			if (convertView == null) 
			{
				convertView = inflater.inflate(R.layout.item_contacto_historial, null);
				holder = new Holder();
				holder.txtTel = (TextView) convertView.findViewById(R.id.txtContacoHistorialNumero);
				holder.imgTipo = (ImageView) convertView.findViewById(R.id.imageViewContacoHistorialTipo);
				holder.txtFecha = (TextView) convertView.findViewById(R.id.txtContacoHistorialFecha);
				holder.imgSubTipo = (ImageView) convertView.findViewById(R.id.imageViewContacoHistorialSubTipo);
				convertView.setTag(holder);
			} 
			else 
			{
				holder = (Holder) convertView.getTag();
			}

			ItemHistorial info = (ItemHistorial) getItem(position);
			
			holder.txtTel.setText(info.getNumero());
			holder.txtFecha.setText(info.getFechaString());
			
			if(info instanceof Llamada)
			{
				holder.imgTipo.setImageDrawable(getResources().getDrawable(R.drawable.ic_realizar_llamada));
			}
			else if (info instanceof Mensaje)
			{
				holder.imgTipo.setImageDrawable(((Mensaje) info).getSubtipoDrawable(HistorialContacto.this));
			}
			else if (info instanceof ItemEmail)
			{
				holder.imgTipo.setImageDrawable(getResources().getDrawable(R.drawable.ic_realizar_email));
			}
			else if (info instanceof MensajeBob)
			{
				holder.txtTel.setText(((MensajeBob) info).getUsuario());
				holder.imgTipo.setImageDrawable(getResources().getDrawable(R.drawable.ic_realizar_mensaje_web_c));
			}
			holder.imgSubTipo.setImageDrawable(info.getTipoDrawable(HistorialContacto.this));
			return convertView;
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}
	
	private class CargarHistorialContactoTask extends AsyncTask<String, Void, Contacto>
	{
		@Override
		protected Contacto doInBackground(String... params) 
		{
			return gestorHistorialContacto.cargarHistorial(contactoID);
		}
		
		protected void onPostExecute(Contacto result)
		{
			((TextView) findViewById(R.id.txtHistorialContactoNombre)).setText(result.getNombre());
			((ImageView) findViewById(R.id.imageViewHistorialContactoImagen)).setImageURI( result.getPhotoUri() );
			adapter.notifyDataSetChanged();
			linearLay.setVisibility(View.GONE);
		}
	}
}
